package com.lance.core.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonUtil {
	
	static Logger LOGGER = LoggerFactory.getLogger(JacksonUtil.class);
	
	static ObjectMapper om = new ObjectMapper();
	
	/** 对象转string */
	public static String objectToStr(Object obj){
		try {
			return om.writeValueAsString(obj);
		} catch (Exception e) {
			LOGGER.error("对象转换异常",e);
			return "";
		}
	}
	
	/** 对象转string */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object strToObject(String msg,Class obj){
		try {
			return om.readValue(msg.getBytes(), obj);
		} catch (Exception e) {
			LOGGER.error("对象转换异常",e);
			return null;
		}
	}
	
	public static <T> T read(String params,Class<T> classs){
		 try {
			return om.readValue(params, classs);
		} catch (Exception e) {
			LOGGER.error("对象转换异常",e);
			return null;
		}
	}
}
